package {
	/* Our framework */
	import org.flixel.*;
	
	/*
	 * 
	 * 
	 */
	public class Enemy extends Ship {
        private static var _recycler:FlxRecycler = new FlxRecycler(100);
        protected var _die_particles:FlxEmitter = new FlxEmitter();
        protected var _blast_particles:FlxEmitter = new FlxEmitter();
		private var _blastoff:Boolean = false;
		public function setBlastOff(BlastOff:Boolean):void {
			_blastoff = BlastOff;
		}

        public function Enemy(x:int, y:int) {
			super(x, y);
            loadGraphic(Resources.enemy_1, false, false, 46, 38, true);
            _default_velocity = -200;
            velocity.x = _default_velocity;
            createDieParticle();
			createBlastParticle();
		}

		override public function reset(X:Number,Y:Number):void {
			createBlastParticle();
			createDieParticle();
			super.reset(X, Y);
		}
		
        protected function createDieParticle():void {
            _die_particles.delay = 0.2;
            _die_particles.gravity = 0;
            _die_particles.maxRotation = 0;
            _die_particles.setXSpeed(-500, 500);
            _die_particles.setYSpeed(-500, 500);
            
            for(var i:int = 0;i < 10;i++) {
                var particle:FlxSprite = new FlxSprite();
                particle.createGraphic(2, 2, 0xFFFFFFFF);
                particle.exists = false;
                _die_particles.add(particle);
            }

            FlxG.state.add(_die_particles);
        }

        protected function createBlastParticle():void {
            _blast_particles.delay = 0.2;
            _blast_particles.gravity = 0;
            _blast_particles.maxRotation = 0;
            _blast_particles.setXSpeed(-500, 500);
            _blast_particles.setYSpeed(-500, 500);
            
            for(var i:int = 0;i < 3;i++) {
                var particle:FlxSprite = new FlxSprite();
                particle.createGraphic(5, 5, 0xFFFF0000);
                particle.exists = false;
                _blast_particles.add(particle);
            }

            StatePlay(FlxG.state).blast_frag.add(_blast_particles);
        }		
				
        override public function update():void {
            super.update();
            velocity.y = Math.cos(x / 50) * 50;
        }

        static public function getEnemy(X:Number = 0, Y:Number = 0):Enemy {
			var e:Enemy 
			
			// add some enemies to avoid a leak
			if (_recycler.members.length == 0) {
				for (var i:uint = 0; i < 10; i++) {
					e = new Enemy(X, Y);
					_recycler.add(e);
				}
			}
			
            if(!_recycler.isMax()) {
                e = new Enemy(X,Y);
                _recycler.add(e);
            }
            e =  _recycler.getObject(X, Y) as Enemy;
			return e;
        }

        override public function kill():void {
			FlxG.play(Resources.explosion_1);
			if(health < 0) {
				_blast_particles.start();
				_blast_particles.at(this);
			}else {
				_die_particles.start();
				_die_particles.at(this);				
			}
		
            super.kill();
        }
    }
}

